package com.byxy.action;

import com.byxy.model.AdminGroup;
import com.byxy.model.AdminRule;
import com.byxy.model.PageModel;
import com.byxy.service.AdminGroupService;
import com.byxy.service.AdminGroupTagService;
import com.byxy.service.AdminRuleService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Controller
@Scope("prototype")
public class AdminGroupAction extends ActionSupport {
    @Autowired
    private AdminGroupService adminGroupService;
    @Autowired
    private AdminGroupTagService adminGroupTagService;
    @Autowired
    private AdminRuleService adminRuleService;

    private AdminGroup adminGroup = new AdminGroup();
    private PageModel pageModel = new PageModel();
    private Integer id[];
    // 页数
    private Integer page;
    // 行数
    private Integer rows;
    // 模糊搜索的关键字
    private String search;
    private Integer adminRuleId[];

    /*
        分页查询视图
    */
    public String list() {
        if (page != null) {
            pageModel.setPage(page);
        }
        if (rows != null) {
            pageModel.setPageSize(rows);
        }
        if (search == null) {
            search = "";
        }
        Integer count = adminGroupService.count(search);
        pageModel.setTotalCount(count);
        List<AdminGroup> list = adminGroupService.listPage(pageModel.getPage(), pageModel.getPageSize(), search);
        pageModel.setItems(list);
        return SUCCESS;
    }

    /*
        增加视图
     */
    public String add() {
        HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
//        request.setAttribute("rules", adminGroupTagService.listRule());
        request.setAttribute("rules", adminRuleService.listPage(0, 0, ""));
        request.setAttribute("pids", adminGroupTagService.listPid());
        return SUCCESS;
    }

    /*
        编辑视图
     */
    public String edit() {
        adminGroup = adminGroupService.get(id[0]);
        HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
//        request.setAttribute("rules", adminGroupTagService.listRule());
        request.setAttribute("rules", adminRuleService.listPage(0, 0, ""));
        request.setAttribute("pids", adminGroupTagService.listPid());
        return SUCCESS;
    }

    /*
        删除操作
     */
    public String doDelete() {
        adminGroupService.delete(id);
        return SUCCESS;
    }

    /*
        启用操作
     */
    public String doEnable() {
        adminGroupService.enable(id);
        return SUCCESS;
    }

    /*
        禁用操作
     */
    public String doDisable() {
        adminGroupService.disable(id);
        return SUCCESS;
    }

    /*
       判断是增加还是更新, 执行对应操作
    */
    public String doAddOrEdit() {
        Set<AdminRule> adminRules = new HashSet<>();
        for (int i : adminRuleId) {
            AdminRule adminRule = adminRuleService.get(i);
            adminRules.add(adminRule);
        }
        adminGroup.setAdminRules(adminRules);
        if (adminGroup.getId() == null || adminGroup.getId() == 0) {
            adminGroupService.save(adminGroup);
        } else {
            adminGroupService.update(adminGroup);
        }
        return SUCCESS;
    }

    /********************下面是get和set********************/

    public AdminGroup getAdminGroup() {
        return adminGroup;
    }

    public void setAdminGroup(AdminGroup adminGroup) {
        this.adminGroup = adminGroup;
    }

    public PageModel getPageModel() {
        return pageModel;
    }

    public void setPageModel(PageModel pageModel) {
        this.pageModel = pageModel;
    }

    public Integer[] getId() {
        return id;
    }

    public void setId(Integer[] id) {
        this.id = id;
    }

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getRows() {
        return rows;
    }

    public void setRows(Integer rows) {
        this.rows = rows;
    }

    public String getSearch() {
        return search;
    }

    public void setSearch(String search) {
        this.search = search;
    }

    public Integer[] getAdminRuleId() {
        return adminRuleId;
    }

    public void setAdminRuleId(Integer[] adminRuleId) {
        this.adminRuleId = adminRuleId;
    }
}
